<template>
  <div class="about">
    <h1>{{ id ? '编辑' : '新建' }}英雄</h1>
    <el-form @submit.native.prevent="save">
      <el-tabs type="border-card" value="basic">
        <el-tab-pane label="基本信息" name="basic">
          <el-form-item label="名称" label-width="100px">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="称号" label-width="100px">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label="头像" label-width="100px">
            <!-- http://moba.artoria-test.xyz/uploads/57477c81dbabdc434bea2fe5d1c765ed -->
            <el-upload class="avatar-uploader" :headers="getAuthHeaders()" :action="$http.defaults.baseURL + '/upload'"
              :show-file-list="false" :on-success="afterUpload">
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="padding" label-width="100px">
            <el-upload class="avatar-uploader" :headers="getAuthHeaders()" :action="$http.defaults.baseURL + '/upload'"
              :show-file-list="false" :on-success="afterBgUpload">
              <img v-if="model.bg" :src="model.bg" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="类型" label-width="100px">
            <el-select v-model="model.categories" multiple>
              <el-option v-for="item of categories" :key="item._id" :value="item._id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度" label-width="100px">
            <el-rate v-model="model.scores.difficulty" show-score style="margin-top:0.8rem" :max="9"></el-rate>
          </el-form-item>
          <el-form-item label="技能" label-width="100px">
            <el-rate v-model="model.scores.skill" show-score style="margin-top:0.8rem" :max="9"></el-rate>
          </el-form-item>
          <el-form-item label="攻击" label-width="100px">
            <el-rate v-model="model.scores.attack" show-score style="margin-top:0.8rem" :max="9"></el-rate>
          </el-form-item>
          <el-form-item label="生存" label-width="100px">
            <el-rate v-model="model.scores.survey" show-score style="margin-top:0.8rem" :max="9"></el-rate>
          </el-form-item>

          <el-form-item label="顺风出装" label-width="100px">
            <el-select v-model="model.items1" multiple>
              <el-option v-for="item of items" :key="item._id" :value="item._id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装" label-width="100px">
            <el-select v-model="model.items2" multiple>
              <el-option v-for="item of items" :key="item._id" :value="item._id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="使用技巧" label-width="100px">
            <el-input type="textarea" v-model="model.usageTips" rows="3"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧" label-width="100px">
            <el-input type="textarea" v-model="model.battleTips" rows="3"></el-input>
          </el-form-item>
          <el-form-item label="团战思路" label-width="100px">
            <el-input type="textarea" v-model="model.teamTips" rows="3"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能" name="skills">
          <el-button size="small" style="margin-bottom:10px" @click="model.skills.push({})"><i
              class="el-icon-plus"></i>添加技能</el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item, i) in model.skills" :key="i">
              <el-form-item label="名称" label-width="70px">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标" label-width="70px">
                <el-upload class="avatar-uploader" :action="$http.defaults.baseURL + '/upload'"
                  :headers="getAuthHeaders()" :show-file-list="false" :on-success="res => $set(item, 'icon', res.url)">
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述" label-width="70px">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item label="冷却值" label-width="70px">
                <el-input type="text" v-model="item.cd"></el-input>
              </el-form-item>
              <el-form-item label="消耗" label-width="70px">
                <el-input type="text" v-model="item.consume"></el-input>
              </el-form-item>
              <el-form-item label-width="70px">
                <el-button type="danger" size="small" @click="model.skills.splice(i, 1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="最佳搭档" name="relation">
          <el-button size="small" style="margin-bottom:10px" @click="model.patners.push({})"><i
              class="el-icon-plus"></i>添加英雄</el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="24" v-for="(item, i) in model.patners" :key="i">
              <el-form-item label="名称" label-width="100px">
                <el-select v-model="item.hero" filterable>
                  <el-option v-for="(hero, i) in heroes" :key="i" :value="hero._id" :label="hero.name"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述" label-width="100px">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>

      <el-form-item style="margin-top:15px">
        <el-button type="primary" native-type="submit" class="save">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {}
  },
  data () {
    return {
      model: {
        scores: {
          difficulty: 0
        },
        skills: [],
        patners: []
      },
      categories: [],
      items: [],
      heroes: []
    }
  },
  methods: {
    async save () {
      let res
      if (this.id) {
        res = await this.$http.put(`rest/heroes/${this.id}`, this.model)
      } else {
        res = await this.$http.post('rest/heroes', this.model)
      }
      // this.$router.push('/heroes/list')
      this.$message({
        type: 'success',
        message: '保存成功'
      })
      console.log(res)
    },
    async fetch () {
      const res = await this.$http.get(`rest/heroes/${this.id}`)
      this.model = Object.assign({}, this.model, res.data)
      console.log(res)
    },
    async fetchCategory () {
      const res = await this.$http.get('rest/categories')
      this.categories = res.data
      console.log(res.data)
    },
    async fetchItems () {
      const res = await this.$http.get('rest/items')
      this.items = res.data
      console.log(res.data)
    },
    async fetchHeroes () {
      const res = await this.$http.get('rest/heroes')
      this.heroes = res.data
      console.log(res.data)
    },
    afterUpload (res) {
      // this.model.icon = res.url
      this.$set(this.model, 'avatar', res.url)
    },
    afterBgUpload (res) {
      console.log(res.url)
      console.log(this.$http.defaults.baseURL)
      this.$set(this.model, 'bg', res.url)
    }
  },
  created () {
    this.fetchItems()
    this.fetchCategory()
    this.fetchHeroes()
    this.id && this.fetch()
  }
}
</script>

<style scoped>
.el-col:nth-child(odd) {
  padding: 10px 30px 10px 0;
  border-right: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
}
.el-col:nth-child(even) {
  padding: 10px 0 10px 10px;
  border-bottom: 1px solid #ccc;
}
</style>
